package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import cn.hutool.core.util.StrUtil;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.BeanCopierUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelTaskCandidateSettings;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelTaskCandidateSettingsDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskCandidateSettingsRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowTaskCandidateSettings;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowTaskCandidateSettingsExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowTaskCandidateSettingsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description 流程任务候选人设置资源库实现方法
 * @author liujinjian
 * @date 2021-10-20
 * @version 1.0
 */
@Repository
public class FlowTaskCandidateSettingsRepositoryImpl implements FlowTaskCandidateSettingsRepository {

    @Autowired
    private FlowTaskCandidateSettingsDao flowTaskCandidateSettingsDao;

    /**
     * @Description 根据主键删除候选人信息记录
     * @author liujinjian
     * @date 2021-10-20
     * @param id
     * @version 1.0
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Long id) {
        return flowTaskCandidateSettingsDao.deleteByPrimaryKey(id);
    }

    /**
     * @Description 插入候选人设置信息记录
     * @author liujinjian
     * @date 2021-10-20
     * @param record
     * @version 1.0
     * @return
     */
    @Override
    public int insert(ModelTaskCandidateSettings record) {
        FlowTaskCandidateSettings entity = new FlowTaskCandidateSettings();
        BeanCopierUtils.copyProperties(record, entity);
        return flowTaskCandidateSettingsDao.insert(entity);
    }

    /**
     * @Description 插入选中的候选者信息记录
     * @author liujinjian
     * @date 2021-10-20
     * @param record
     * @version 1.0
     * @return
     */
    @Override
    public int insertSelective(ModelTaskCandidateSettings record) {
        FlowTaskCandidateSettings entity = new FlowTaskCandidateSettings();
        BeanCopierUtils.copyProperties(record, entity);
        return flowTaskCandidateSettingsDao.insertSelective(entity);
    }

    /**
     * @Description 根据主键查询候选人信息记录
     * @author liujinjian
     * @date 2021-10-20
     * @param id
     * @version 1.0
     * @return
     */
    @Override
    public ModelTaskCandidateSettings selectByPrimaryKey(Long id) {
        ModelTaskCandidateSettings entity = new ModelTaskCandidateSettings();
        FlowTaskCandidateSettings record = flowTaskCandidateSettingsDao.selectByPrimaryKey(id);
        BeanCopierUtils.copyProperties(record,entity);
        return entity;
    }

    /**
     * @Description 更新选中的候选人信息记录
     * @author liujinjian
     * @date 2021-10-20
     * @param record
     * @version 1.0
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(ModelTaskCandidateSettings record) {
        FlowTaskCandidateSettings entity = new FlowTaskCandidateSettings();
        BeanCopierUtils.copyProperties(record, entity);
        return flowTaskCandidateSettingsDao.updateByPrimaryKeySelective(entity);
    }

    /**
     * @Description 根据主键更新Blob内容信息
     * @author liujinjian
     * @date 2021-10-20
     * @param record
     * @version 1.0
     * @return
     */
    @Override
    public int updateByPrimaryKeyWithBLOBs(ModelTaskCandidateSettings record) {
        FlowTaskCandidateSettings entity = new FlowTaskCandidateSettings();
        BeanCopierUtils.copyProperties(record, entity);
        return flowTaskCandidateSettingsDao.updateByPrimaryKeyWithBLOBs(entity);
    }

    /**
     * @Description 根据主键更新流程任务候选人信息记录
     * @author liujinjian
     * @date 2021-10-20
     * @param record
     * @version 1.0
     * @return
     */
    @Override
    public int updateByPrimaryKey(ModelTaskCandidateSettings record) {
        FlowTaskCandidateSettings entity = new FlowTaskCandidateSettings();
        BeanCopierUtils.copyProperties(record, entity);
        return flowTaskCandidateSettingsDao.updateByPrimaryKey(entity);
    }

    /**
     * @Description 保存模型或自定义配置信息
     * @author liujinjian
     * @date 2021-10-20
     * @param modelTaskCandidateSettingsDomain
     * @version 1.0
     * @return boolean
     */
    @Override
    public boolean save(ModelTaskCandidateSettingsDomain modelTaskCandidateSettingsDomain) {
        FlowTaskCandidateSettings candidateSettings = new FlowTaskCandidateSettings();
        BeanCopierUtils.copyProperties(modelTaskCandidateSettingsDomain,candidateSettings);
        return flowTaskCandidateSettingsDao.insert(candidateSettings)>0;
    }

    /**
     * @Description 添加任务部署候选人设置信息
     * @author liujinjian
     * @date 2021-10-20
     * @param modelTaskCandidateSettings
     * @version 1.0
     * @return
     */
    @Override
    public boolean addTaskDeployCandidateSettingsInfo(ModelTaskCandidateSettings modelTaskCandidateSettings) {
        FlowTaskCandidateSettings entity = new FlowTaskCandidateSettings();
        BeanCopierUtils.copyProperties(modelTaskCandidateSettings, entity);
        return flowTaskCandidateSettingsDao.insertSelective(entity)>0;
    }

    /**
     * @Description 根据部署ID和节点编号获取模型任务候选人设置信息集合
     * @author liujinjian
     * @date 2021-10-20
     * @param deployId
     * @param nodeCode
     * @return List<ModelTaskCandidateSettings>
     */
    @Override
    public List<ModelTaskCandidateSettings> listModelTaskCandidateSettings(String deployId, String nodeCode) {
        FlowTaskCandidateSettingsExample query = new FlowTaskCandidateSettingsExample();
        FlowTaskCandidateSettingsExample.Criteria criteria = query.createCriteria().andDeployIdEqualTo(deployId);
        criteria.andNodeCodeEqualTo(nodeCode);

        List<FlowTaskCandidateSettings> configs = flowTaskCandidateSettingsDao.selectByExampleWithBLOBs(query);

        return configs.stream().map(this::convert).collect(Collectors.toList());
    }

    /**
     * @Description 根据部署ID和节点编号获取模型任务候选人设置信息集合
     * @author liujinjian
     * @date 2021-10-20
     * @param deployId
     * @param nodeCode
     * @return List<ModelTaskCandidateSettingsDomain>
     */
    @Override
    public List<ModelTaskCandidateSettingsDomain> listModelTaskCandidateSettingsByDeployIdAndNodecode(String deployId, String nodeCode) {
        FlowTaskCandidateSettingsExample query = new FlowTaskCandidateSettingsExample();
        FlowTaskCandidateSettingsExample.Criteria criteria = query.createCriteria().andDeployIdEqualTo(deployId);
        if (StrUtil.isNotEmpty(nodeCode)) {
            criteria.andNodeCodeEqualTo(nodeCode);
        }

        List<FlowTaskCandidateSettings> configs = flowTaskCandidateSettingsDao.selectByExampleWithBLOBs(query);

        return configs.stream().map(this::convertToDoamain).collect(Collectors.toList());
    }

    /**
     * @Description 根据部署ID和节点编号及是否已选中操作标识获取模型任务候选人设置信息集合
     * @author liujinjian
     * @date 2021-11-21
     * @param deployId
     * @param nodeCode
     * @param isPick
     * @return
     */
    @Override
    public List<ModelTaskCandidateSettingsDomain> getModelTaskCandidateSettingsList(String deployId, String nodeCode, String isPick) {
        FlowTaskCandidateSettingsExample query = new FlowTaskCandidateSettingsExample();
        FlowTaskCandidateSettingsExample.Criteria criteria = query.createCriteria().andDeployIdEqualTo(deployId);
        criteria.andNodeCodeEqualTo(nodeCode);
        criteria.andIsPickEqualTo(isPick);

        List<FlowTaskCandidateSettings> configs = flowTaskCandidateSettingsDao.selectByExampleWithBLOBs(query);

        return configs.stream().map(this::convertToDoamain).collect(Collectors.toList());
    }

    private ModelTaskCandidateSettings convert(FlowTaskCandidateSettings record) {
        if (Objects.isNull(record)) {
            return null;
        }
        ModelTaskCandidateSettings config = new ModelTaskCandidateSettings();
        BeanCopierUtils.copyProperties(record, config);
        return config;
    }

    private ModelTaskCandidateSettingsDomain convertToDoamain(FlowTaskCandidateSettings record){
        if (Objects.isNull(record)) {
            return null;
        }
        ModelTaskCandidateSettingsDomain config = new ModelTaskCandidateSettingsDomain();
        BeanCopierUtils.copyProperties(record, config);
        return config;
    }
}